package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.miaodi.GetMessageCode;
import com.cx.pojo.User;
import com.cx.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RequestMapping("user")
@Controller
@CrossOrigin
public class UserController {
    @Autowired
    private UserCenterService userService;
    UserCenterService userCenterService;

    //    用户登录
    @RequestMapping("login")
    @ResponseBody
    public ApiReturn login(User user, HttpServletRequest request, HttpServletResponse response) {
        //获取session中的验证码
        String code = (String) request.getSession().getAttribute("code");
        if (user.getOther2() != null) {
            if (code.equals(user.getOther2())) {
                user.setOther2(null);
                String result = userService.yzmlogin(user);
                if (result.equals("用户不存在")) {
                    return ApiReturn.error("该手机号未注册！");
                }
//        携带用户信息  result为： id:手机号
                request.getSession().setAttribute("userInfo", result);
                return ApiReturn.success("返回用户登录数据", result);
            } else {
                return ApiReturn.error("验证码错误");
            }
        }
        if (user.getOther2() == null) {
            String result = userService.login(user);
            if (result.equals("用户不存在")) {
                return ApiReturn.error("该手机号未注册！");
            }
            if (result.equals("密码错误")){
                return ApiReturn.error("密码错误");
            }
            //        携带用户信息  result为： id:手机号
            request.getSession().setAttribute("userInfo", result);
            return ApiReturn.success("返回用户登录数据", result);
        }
        return ApiReturn.error("用户不存在");
    }

    //    用户登录信息
    @RequestMapping("getUserInfo")
    @ResponseBody
    public ApiReturn getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        //获取session中的用户信息
        String userInfo = (String) request.getSession().getAttribute("userInfo");
        if (userInfo == null || userInfo == "") {
            return ApiReturn.error("未登录");
        }
        String[] userStr = userInfo.split(":");
        User user = new User();
        user.setUserId(Integer.valueOf(userStr[0]));
        user.setUserName(userStr[1]);
        return ApiReturn.success("返回用户登录数据", user);
    }

    //    用户注册
    @RequestMapping("register")
    @ResponseBody
    public ApiReturn register(User user, HttpServletRequest request, HttpServletResponse response) {
        //获取session中的验证码
        String code = (String) request.getSession().getAttribute("code");
        if (code.equals(user.getOther2())) {
            user.setOther2(null);
            int count = userService.insert(user);
            if (count >= 1) {
                return ApiReturn.success("注册成功");
            }
            return ApiReturn.error("注册失败");
        }
        return ApiReturn.error("验证码错误");
    }

    @RequestMapping("yzm")
    @ResponseBody
    public ApiReturn getCode(String userPhone, HttpServletRequest request, HttpServletResponse response) {
        String mobile = userService.selectPhone(userPhone);
        if (mobile != null && mobile.equals(userPhone)) {
            return ApiReturn.error("该手机号已被注册");
        } else {
            String code = null;
            try {
                code = GetMessageCode.getCode(userPhone);
                // 携带验证码
                request.getSession().setAttribute("code", code);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ApiReturn.success("返回验证码", code);
        }
    }

    @RequestMapping("loginyzm")
    @ResponseBody
    public ApiReturn yzm(String userPhone, HttpServletRequest request, HttpServletResponse response) {
        String mobile = userService.selectPhone(userPhone);
        if (mobile != null) {
            String code = "";
            try {
                code = GetMessageCode.getCode(userPhone);
                // 携带验证码
                request.getSession().setAttribute("code", code);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ApiReturn.success("返回验证码", code);
        } else {
            return ApiReturn.error("该手机号未注册！");
        }
    }
}
